<script>
export default {
  name:"roleManage",
  data() {
    let checkRoleName =(rule,value,callback)=>{
      if(this.form.id){
        return callback();
      }
      this.$axios.post("/zzh-role/findeByName?name="+this.form.loginName).then(res=>res.data).then(res=>{
        console.log(res.data)
        if(res.resultCode!=200){
          callback()
        }else{
          callback(new Error('登录账号已经存在'));
        }
      })
    };
    return {
      tableData: [],
      pageNum:1,
      pageSize:5,
      total:5,
      name:'',
      id:'',
      dialogVisible:false,
      dialogMenuVisible:false,
      loading: true,
      menuList:[],
      defaultCheckedKeys:[],
      selectedNodes: [],
      defaultProps: {
        valueL:'number',
        children: 'children',
        label: 'name'
      },
      form:{
        id:'',
        name:'',
        type:'',
        sort:'',
        description:'',
        status:'1'
      },
      rules: {
        name: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          { min: 3, max: 18, message: '长度在 3 到 18 个字符', trigger: 'blur' },
          {validator:checkRoleName,trigger: 'blur'}
        ],
        type: [
          { required: true, message: '请输选择类型', trigger: 'change' }
        ],
        description: [
          { required: true, message: '请输入备注', trigger: 'change' }
        ],
        sort: [
          { required: true, message: '请输入排序', trigger: 'change' }
        ],
      }
    };
  },
  methods:{
    //查询所有角色信息
    loadPost(){
      this.loading = true;
      this.$axios.post('/zzh-role/list',{
        page:this.pageNum,
        limit:this.pageSize,
        name:this.name
      }).then(res=>res.data).then(res=>{
        this.loading = false;
        if(res.resultCode==200){
          this.tableData=res.data
          this.total=res.total
        }else {
          alert('获取数据失败')
        }
      })
    },
    //判断点击了修改按钮还是添加按钮
    loadAdd(){
      //表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          if(this.form.id){
            this.doUpdate()
            this.form.id=''
          }else {
            this.doAdd()
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    //进行添加操作
    doAdd(){
      this.$axios.post('/zzh-role/add',this.form).then(res=>res.data).then(res=>{
        if(res.resultCode==200){
          this.dialogVisible=false
          this.$message({
            showClose: true,
            message: '新增数据成功',
            type: 'success'
          });
          this.loadPost()
        }else {
          this.$message({
            showClose: true,
            message: res.message,
            type: 'error'
          });
        }
      })
    },
    //进行更新操作
    doUpdate(){
      this.$axios.put('/zzh-role/update',this.form).then(res=>res.data).then(res=>{
        if(res.resultCode==200){
          this.dialogVisible=false
          this.$message({
            showClose: true,
            message: '更新数据成功',
            type: 'success'
          });
          this.loadPost()
        }else {
          this.$message({
            showClose: true,
            message: res.message,
            type: 'error'
          });
        }
      })
    },
    //分页操作
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageNum=1
      this.pageSize=val
      this.loadPost()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum=val
      this.loadPost()
    },
    //重新刷新页面
    resetParam(){
      this.name=''
      this.department=''
      this.loadPost()
    },
    //添加按钮
    add(){
      this.dialogVisible=true
      this.$nextTick(()=>{
        this.resetForm()
      })
    },
    //赋值操作
    mod(row){
      this.dialogVisible=true
      this.$nextTick(()=>{
        this.form.id=row.id
        this.form.name=row.name
        this.form.type=row.type
        this.form.sort=row.sort
        this.form.description=row.description
      })
    },
    //进行删除操作
    del(id){
      this.$axios.delete('/zzh-role/delete?id='+id).then(res=>res.data).then(res=>{
        if(res.resultCode==200){
          this.$message({
            showClose: true,
            message: '删除成功',
            type: 'success'
          });
          this.loadPost()
        }else {
          this.$message({
            showClose: true,
            message: res.message,
            type: 'error'
          });
        }
      })
    },
    //进行更新角色状态
    updateStatus(id){
      this.$axios.put('/zzh-role/updateStatus?id='+id).then(res=>res.data).then(res=>{
        if(res.resultCode==200){
          this.$message({
            showClose: true,
            message: '更新状态成功',
            type: 'success'
          });
          this.loadPost()
        }else {
          this.$message({
            showClose: true,
            message: res.message,
            type: 'error'
          });
        }
      })
    },
    //展示角色功能按钮
    loadPushBtn(id){
      this.id=id
      this.dialogMenuVisible=true
      this.$axios.post('/zzh-function/findeByIdList?roleId='+id).then(res=>res.data).then(res=>{
        if(res.resultCode==200){
          this.defaultCheckedKeys=res.data
        }else{
          this.dialogMenuVisible=false;
          return false;
        }
      });
      //查询角色功能信息列表
      this.$axios.post('/zzh-function/findeByList').then(res=>res.data).then(res=>{
        if(res.resultCode==200){
          this.menuList=res.data
        }else{
          this.dialogMenuVisible=false;
          alert('菜单设置失败！');
          return false;
        }
      });
    },
    //提交角色功能按钮
    pushBtn(){
      const checkedNodes = this.$refs.tree.getCheckedNodes();
      const ids=[];
      // 使用 forEach 方法遍历
      checkedNodes.forEach(node => {
        ids.push(node.id);
      });
      console.log(ids);
      this.$axios.post('/zzh-function/pushBtn', {ids:ids,id:this.id}).then(res=>res.data).then(res=>{
        this.dialogMenuVisible=false;
        if(res.resultCode==200){
          this.$message({
            showClose: true,
            message: '分配按钮成功',
            type: 'success'
          });
        }else {
          this.$message({
            showClose: true,
            message: '分配按钮失败',
            type: 'error'
          });
        }
      })
    },
    //重置窗体
    resetForm() {
      this.$refs.form.resetFields();
    }
  },
  beforeMount() {
    this.loadPost();
  }
}
</script>

<template>
  <div>
    <div style="margin-bottom: 5px">
      <el-input v-model="name" placeholder="请输入角色名称" suffix-icon="el-icon-search" style="width: 200px"
                @keyup.enter.native="loadPost"></el-input>
      <el-button type="primary" style="margin-left: 5px" @click="loadPost">查询</el-button>
      <el-button type="success" @click="resetParam">重置</el-button>
      <el-button type="primary" style="margin-left: 5px" @click="add">新增</el-button>
    </div>

    <el-table :data="tableData" v-loading="loading"
              :header-cell-style="{background:'#f3f6fd',color:'#555'}">
      <el-table-column prop="id" label="id" width="140">
      </el-table-column>
      <el-table-column prop="name" label="角色名称" width="140">
      </el-table-column>
      <el-table-column prop="type" label="类型" width="120">
      </el-table-column>
      <el-table-column prop="description" label="角色描述" width="120">
      </el-table-column>
      <el-table-column prop="status" label="启用状态" width="120">
        <template slot-scope="scope">
          <el-tag
              :type="scope.row.status === 1 ? 'success' : 'danger'"
              disable-transitions>{{scope.row.status === 1 ? '正常' : scope.row.status === 2 ? '禁用' : '已删除'}}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="operate" label="操作">
        <template slot-scope="scope">
          <span v-if="scope.row.id !== 1">
            <el-button size="small" type="success" @click="mod(scope.row)">编辑</el-button>
            <el-popconfirm
                title="确定删除吗？"
                style="margin-left: 5px;margin-right: 5px"
                @confirm="del(scope.row.id)"
            >
              <el-button slot="reference" size="small" type="danger">删除</el-button>
            </el-popconfirm>
            <el-button size="small" type="success" @click="updateStatus(scope.row.id)">启用/禁用</el-button>
            <el-button size="small" type="success" @click="loadPushBtn(scope.row.id)">分配按钮</el-button>
          </span>
        </template>
      </el-table-column>
    </el-table>

<!--    页面跳转选择-->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
<!--弹出框-->
    <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="40%"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" size="mini">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入登录名称"></el-input>
        </el-form-item>
        <el-form-item label="角色类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择角色" style="width: 100%">
            <el-option label="全部数据" value="全部数据"></el-option>
            <el-option label="本机构数据" value="本机构数据"></el-option>
            <el-option label="个人数据" value="个人数据"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="角色描述" placeholder="请输入角色描述信息" prop="description">
          <el-input v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item label="排序" placeholder="请输入排序" prop="sort">
          <el-input v-model="form.sort"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="loadAdd">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
        title="提示"
        :visible.sync="dialogMenuVisible"
        width="40%"
    >
      <el-tree
          ref="tree"
          node-key="id"
          :data="menuList"
          :props="defaultProps"
          :v-model="selectedNodes"
          :check-strictly="true"
          :default-checked-keys="defaultCheckedKeys"
          default-expand-all
          show-checkbox>
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogMenuVisible = false">取 消</el-button>
        <el-button type="primary" @click="pushBtn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>